package com.abcdefg.rabbitmqextensionsdemo.listener;

import com.abcdefg.rabbitmqextensionsdemo.constants.Constant;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class QusListener {
    @RabbitListener(queues = Constant.QUS_QUEUE)
    public void listen1(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {

            System.out.printf("111接收到的消息：%s,deliveryTag：%d\n", new String(message.getBody())
                    , message.getMessageProperties().getDeliveryTag());
            Thread.sleep(3000);
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            //进行异常确认
            channel.basicNack(deliveryTag,false,false);
        }
    }

    @RabbitListener(queues = Constant.QUS_QUEUE)
    public void listen2(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {

            System.out.printf("222接收到的消息：%s,deliveryTag：%d\n", new String(message.getBody())
                    , message.getMessageProperties().getDeliveryTag());
            Thread.sleep(1000);
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            //进行异常确认
            channel.basicNack(deliveryTag,false,false);
        }
    }
}
